package org.neframework.webtest.action;

import java.util.Map;

import org.neframework.webtest.base.BaseController;
import org.neframework.webtest.model.t_fk_user;
import org.neframework.webtest.model.t_s_code;
import org.neframework.webtest.service.CodeService;
import org.neframework.webtest.service.UserService;
import org.neframework.webtest.service.VerifyService;
import org.neframework.webtest.util.verify.VerifyTools;
import org.neframework.webtest.util.web.session.SessionTools;

import com.tmsps.neframework.core.utils.ChkTools;
import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.core.utils.MD5;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
public class LoginController extends BaseController {

	private static UserService userService = new UserService();
	private static VerifyService verifyService = new VerifyService();
	private static CodeService codeService = new CodeService();

	@RequestMapping("/login")
	public String login(String uname, String pwd) {

		if (ChkTools.isNull(uname) && ChkTools.isNull(pwd)) {
			return "/jsp/login.jsp";
		}

		t_fk_user user = userService.selectUserByEmailAndPwd(uname,
				MD5.md5(pwd));
		if (user != null) {
			SessionTools.put(SessionTools.LOGIN_ADMIN, user, req);

			return "redirect:/cp/frame/frame.htm";
		} else {
			super.setTipMsg("登录失败，账号密码错误！", Tip.Type.error);
			return "/jsp/login.jsp";
		}
	}

	@RequestMapping("/logout")
	public String logout() {

		req.getSession().invalidate();
		super.setTipMsg("退出成功！", Tip.Type.success);
		return "redirect:/login.htm";
	}

	@RequestMapping("/relogin")
	public String relogin() {
		req.getSession().invalidate();
		return "/jsp/login/relogin.jsp";
	}

	// 校验
	@RequestMapping("/verify_form")
	public String verify_form() {
		req.getSession().invalidate();
		return "/jsp/login/verify_form.jsp";
	}

	@RequestMapping("/verify")
	public String verify(String code) {

		Map<String, Object> codeInfo = VerifyTools.getVerifyInfo(code);
		Long enddate = (Long) codeInfo.get("enddate");
		if (enddate == null || enddate <= System.currentTimeMillis()) {
			super.setTipMsg("校验失败,序列号已过期！", Tip.Type.error);
			return "redirect:/verify_form.htm";
		}

		boolean b = verifyService.updateVerifyInfo(code, enddate);

		if (b) {
			super.setTipMsg("校验成功！请登录!", Tip.Type.success);
			return "redirect:/login.htm";
		} else {
			super.setTipMsg("校验失败,序列号已过期！", Tip.Type.error);
			return "redirect:/verify_form.htm";

		}
	}

	// 公网使用
	@RequestMapping("/get_verify_info")
	@ResponseBody
	public String get_verify_info(String code) {
		t_s_code codeInfo = codeService.selectCode(code);

		return JsonTools.toJson(codeInfo);
	}

	// 公网使用
	@RequestMapping("/set_verify_info")
	@ResponseBody
	public String set_verify_info(String code) {
		t_s_code codeInfo = codeService.selectCode(code);
		if ("待验证".equals(codeInfo.getStatus_verify())) {
			codeInfo.setStatus_verify("已验证");
			codeInfo.setVerify_ip(req.getLocalAddr());
			oc.updateObj(codeInfo);

			result.put("end", "true");
		} else {
			result.put("end", "false");
		}

		return JsonTools.toJson(result);
	}

}
